
import java.net.*;
import java.util.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Vladiir
 */
public class UDPPingerClient {

    public static void main( String args[] ) throws Exception
    {
        final String CRLF = "\r\n";
        DatagramSocket clientSocket = new DatagramSocket();
        clientSocket.setSoTimeout(1000);
        InetAddress IPAddress = InetAddress.getByName("localhost");
        String mensagem;
        Date data;
        long tempo1 = 0;
        long tempo2 = 0;
        
        for(int i = 0; i < 10 ; i ++)
        {
            try{
            data = new Date(System.currentTimeMillis());
            mensagem = "PING "+ i + " " + data.toString()+ " " + CRLF;
            byte[] sendData = new byte[mensagem.getBytes().length];
            byte[] receiveData = new byte[mensagem.getBytes().length];
            sendData = mensagem.getBytes();
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 1234);
            tempo1 = System.currentTimeMillis();
            clientSocket.send(sendPacket);
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            clientSocket.receive(receivePacket);
            tempo2 = System.currentTimeMillis();
            System.out.println(new String(receiveData));
            System.out.println("Tempo total " + (tempo2 - tempo1));}

            catch ( Exception e)
            {
                System.out.println(e);
            }
        }
        clientSocket.close();
    }

}
